package dataBase;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import org.mortbay.log.Log;

import com.google.appengine.api.datastore.Key;


public class AllBiz {
	private  HashMap<String,String> bizHash;
	private HashMap<String, Long> keyHash;
	private  ArrayList<String> allBizNameArray;
	private  ArrayList<String> allBizUserNameArray;
	static AllBiz inst = new AllBiz();
	private AllBiz() {	
		updateAllBiz();
	}
	
	public static AllBiz getInst(){
//		if (inst==null)
//			inst = new AllBiz();
		return inst;
	}
	
	private void updateAllBiz()
	{
		bizHash = new HashMap<String, String>();
		keyHash = new HashMap<String, Long>();
		allBizNameArray = new ArrayList<String>();
		allBizUserNameArray = new ArrayList<String>();
		List<Business> biz = ConnectToDataBase.getInst().allBiz();
		
		if (biz!=null)
		{
			for (Business b: biz)
			{
				Logger.getLogger("home.jsp").warning("Business: "+b);
				if(b!=null){
					Logger.getLogger("home.jsp").warning("Business: "+b.getUserName());
					bizHash.put(b.getBizName(), b.getUserName());
					keyHash.put(b.getBizName(), b.getKey().getId());
					System.out.println("bizName: "+b.getBizName());
					allBizNameArray.add(b.getBizName());
					allBizUserNameArray.add(b.getUserName());
				}
			}
		}
		
		
	}
	
	public  ArrayList<String> allBizNameArray()
	{
//		if (bizHash==null)
//			updateAllBiz();
		
		return allBizNameArray;
	}
	
	public  ArrayList<String> allBizUserNameArray()
	{

//		if (bizHash==null)
//			updateAllBiz();
		
		return allBizUserNameArray;
	}
	
	public  String bizNameToUserName(String bizName)
	{
//		if (bizHash==null)
//			updateAllBiz();
		
		return bizHash.get(bizName);
	}

	public  Long bizNameToKeyID(String bizName)
	{
		//System.out.println("keyHash "+keyHash);
		//System.out.println("bizName "+bizName);
		return keyHash.get(bizName);
	}
	
	public  String userNameToBizName(String bizUserName)
	{
//		if (bizHash==null)
//			updateAllBiz();
		
		int index = allBizUserNameArray.indexOf(bizUserName);
		
		if (index==-1)
			return null;
		
		return allBizNameArray.get(index);
	}
	
	private int getIndexToAddBizName(String bizName)
	{
		int i;
		int size = allBizNameArray.size();
		for (i=0; i<size; i++)
		{
			//if the current string is bigger than our bizName - we stop
			if (allBizNameArray.get(i).compareTo(bizName)>0)
				break;			
		}
		
		return i;
	}
	
	//assume the is no such biz
	public void addNewBiz(String bizUserName,String bizName,Key key)
	{
		int index = getIndexToAddBizName(bizName);
		
		allBizNameArray.add(index, bizName);
		allBizUserNameArray.add(index, bizUserName);	
		bizHash.put(bizName, bizUserName);
		if(key!=null)
			keyHash.put(bizName,key.getId());
	}
	
	public void updateExistBiz(String bizUserName,String bizName,String prevBizName)
	{
		//there is no change
		if (bizName.equals(prevBizName))
			return;
		
		//delete the previous record of the business (if exists)
		int index = allBizUserNameArray.indexOf(bizUserName);
				
		if (index!=-1)
		{
			bizHash.remove(prevBizName);
			allBizNameArray.remove(index);
			allBizUserNameArray.remove(index);
		}
		
		//add this biz as new biz
		addNewBiz(bizUserName, bizName,null);	
		
	}
	
	
	
}





